// 资产管理相关API
const baseUrl = 'http://localhost:8081'; // 后端接口地址

// 获取资产统计信息
export const getAssetStatistics = () => {
	return uni.request({
		url: baseUrl + '/system/asset/statistics',
		method: 'GET',
		header: {
			'Authorization': uni.getStorageSync('token') || ''
		}
	}).then(res => {
		if (res.data && res.data.code === 200) {
			return {
				code: 200,
				data: {
					total: res.data.data.total || 0,
					normal: res.data.data.normal || 0,
					fault: res.data.data.fault || 0,
					idle: res.data.data.idle || 0,
					scrapped: res.data.data.scrapped || 0,
					maintenance: res.data.data.maintenance || 0
				}
			};
		} else {
			// 如果接口失败，返回默认数据
			return {
				code: 200,
				data: {
					total: 0,
					normal: 0,
					fault: 0,
					idle: 0,
					scrapped: 0,
					maintenance: 0
				}
			};
		}
	}).catch(error => {
		console.error('获取资产统计失败:', error);
		// 返回默认数据
		return {
			code: 200,
			data: {
				total: 0,
				normal: 0,
				fault: 0,
				idle: 0,
				scrapped: 0,
				maintenance: 0
			}
		};
	});
};

// 查询资产列表
export const getAssetList = (params) => {
	return new Promise((resolve) => {
		// 模拟查询资产列表
		setTimeout(() => {
			resolve({
				code: 200,
				data: {
					rows: [
						{
							assetNo: 'AS001',
							deviceNo: 'DKDKDEREWE',
							name: '一车间设备',
							equipmentTypeCode: 'EQ_TYPE_001',
							equipmentTypeName: '数控机床',
							modelNo: 'CNC-001',
							brand: '西门子',
							location: '一车间',
							deptId: 'DEPT001',
							deptName: '生产部',
							supplierName: '设备供应商',
							manufacturer: '西门子',
							purchaseDate: '2020-01-15',
							commissioningDate: '2020-02-01',
							operatorName: '张三',
							statusCode: 'ASSET_STATUS_001',
							statusName: '正常',
							createTime: '2020-01-15 10:00:00'
						},
						{
							assetNo: 'AS002',
							deviceNo: 'DKDKDEREWE',
							name: '二车间设备',
							equipmentTypeCode: 'EQ_TYPE_001',
							equipmentTypeName: '数控机床',
							modelNo: 'CNC-002',
							brand: '发那科',
							location: '二车间',
							deptId: 'DEPT001',
							deptName: '生产部',
							supplierName: '设备供应商',
							manufacturer: '发那科',
							purchaseDate: '2020-03-20',
							commissioningDate: '2020-04-01',
							operatorName: '李四',
							statusCode: 'ASSET_STATUS_002',
							statusName: '故障',
							createTime: '2020-03-20 14:30:00'
						}
					],
					total: 2
				}
			});
		}, 500);
	});
};

// 获取资产详情
export const getAssetDetail = (assetNo) => {
	return new Promise((resolve) => {
		// 模拟获取资产详情
		setTimeout(() => {
			resolve({
				code: 200,
				data: {
					assetNo: assetNo,
					deviceNo: 'DKDKDEREWE',
					name: '一车间设备',
					equipmentTypeCode: 'EQ_TYPE_001',
					equipmentTypeName: '数控机床',
					modelNo: 'CNC-001',
					brand: '西门子',
					location: '一车间',
					deptId: 'DEPT001',
					deptName: '生产部',
					supplierName: '设备供应商',
					manufacturer: '西门子',
					purchaseDate: '2020-01-15',
					commissioningDate: '2020-02-01',
					operatorName: '张三',
					statusCode: 'ASSET_STATUS_001',
					statusName: '正常',
					imageUrls: ['/static/device1.jpg', '/static/device2.jpg'],
					createTime: '2020-01-15 10:00:00'
				}
			});
		}, 500);
	});
};

// 新增资产
export const addAsset = (data) => {
	return new Promise((resolve) => {
		// 模拟新增资产
		setTimeout(() => {
			resolve({
				code: 200,
				message: '资产添加成功',
				data: {
					assetNo: 'AS' + Date.now()
				}
			});
		}, 1000);
	});
};

// 更新资产
export const updateAsset = (data) => {
	return new Promise((resolve) => {
		// 模拟更新资产
		setTimeout(() => {
			resolve({
				code: 200,
				message: '资产更新成功'
			});
		}, 1000);
	});
};

// 删除资产
export const deleteAsset = (assetNo) => {
	return new Promise((resolve) => {
		// 模拟删除资产
		setTimeout(() => {
			resolve({
				code: 200,
				message: '资产删除成功'
			});
		}, 1000);
	});
};

// 批量更新资产状态
export const batchUpdateAssetStatus = (assetNos, statusCode) => {
	return new Promise((resolve) => {
		// 模拟批量更新资产状态
		setTimeout(() => {
			resolve({
				code: 200,
				message: '批量更新成功'
			});
		}, 1000);
	});
};

// 获取资产维修历史
export const getAssetRepairHistory = (assetNo, params) => {
	return new Promise((resolve) => {
		// 模拟获取资产维修历史
		setTimeout(() => {
			resolve({
				code: 200,
				data: {
					rows: [
						{
							id: 1,
							reportNo: 'RPT001',
							workNo: 'WO001',
							faultDescription: '设备异常噪音',
							statusName: '已完成',
							startTime: '2021-03-23 15:00:00',
							endTime: '2021-03-23 16:00:00',
							operatorName: '杨文浩'
						},
						{
							id: 2,
							reportNo: 'RPT002',
							workNo: 'WO002',
							faultDescription: '温度异常',
							statusName: '已完成',
							startTime: '2021-03-20 10:00:00',
							endTime: '2021-03-20 12:00:00',
							operatorName: '张三'
						}
					],
					total: 2
				}
			});
		}, 500);
	});
};

// 获取设备类型列表
export const getEquipmentTypeList = () => {
	return new Promise((resolve) => {
		// 模拟获取设备类型列表
		setTimeout(() => {
			resolve({
				code: 200,
				data: {
					rows: [
						{ code: 'EQ_TYPE_001', name: '数控机床' },
						{ code: 'EQ_TYPE_002', name: '普通机床' },
						{ code: 'EQ_TYPE_003', name: '检测设备' },
						{ code: 'EQ_TYPE_004', name: '辅助设备' }
					]
				}
			});
		}, 300);
	});
};

// 获取资产状态列表
export const getAssetStatusList = () => {
	return new Promise((resolve) => {
		// 模拟获取资产状态列表
		setTimeout(() => {
			resolve({
				code: 200,
				data: {
					rows: [
						{ code: 'ASSET_STATUS_001', name: '正常', color: '#4caf50' },
						{ code: 'ASSET_STATUS_002', name: '故障', color: '#f44336' },
						{ code: 'ASSET_STATUS_003', name: '闲置', color: '#ff9800' },
						{ code: 'ASSET_STATUS_004', name: '报废', color: '#9e9e9e' }
					]
				}
			});
		}, 300);
	});
};

// 搜索资产
export const searchAssets = (keyword) => {
	return new Promise((resolve) => {
		// 模拟搜索资产
		setTimeout(() => {
			resolve({
				code: 200,
				data: {
					rows: [
						{
							assetNo: 'AS001',
							name: '一车间设备',
							location: '一车间',
							statusName: '正常'
						}
					],
					total: 1
				}
			});
		}, 500);
	});
};
